
import { CommonActions, createNavigationContainerRef } from '@react-navigation/native';
import { StackActions } from '@react-navigation/native';

export const navigationRef = createNavigationContainerRef()

export function navigate(name, params) {
  if (navigationRef.isReady()) {
    navigationRef.navigate(name, params);
  }
}

export function push(...args) {
  if (navigationRef.isReady()) {
    navigationRef.dispatch(StackActions.push(...args));
  }
}

export function reset (routeName,params = {}) {
  navigationRef.dispatch(
    CommonActions.reset({
      index: 1,
      routes: [
        {
          name:routeName,
          params: params,
        },
      ],
    })
  );
}

export function replace (routeName, params = {}) {
  navigationRef.dispatch(
    StackActions.replace(routeName,params)
  );
}

export function goBack () {
  navigationRef.dispatch(CommonActions.goBack());
}

export function getCurrentRoute () {

  if (navigationRef.isReady()) {
    const currentRoute = navigationRef.getCurrentRoute();
    return currentRoute?.name;
  }
  return null;
}